package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.auth;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.auth.RoleBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.auth.RoleInfoFunctionBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.auth.RoleInfoFunctionWebBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.util.PermissionCheck;
import com.xiaoxu.intranetweb.util.SzResult;
import com.xiaoxu.intranetweb.util.WebFunctionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@CrossOrigin()
@RestController
@RequestMapping(value = "/function")
/**
 * 权限表控制层
 * @param
 * @param
 */
public class RoleInfoController {
    @Autowired
    private RoleInfoFunctionBu roleInfoFunctionBu;
    @Autowired
    private RoleBu roleBu;
    @Autowired
    private RoleInfoFunctionWebBu roleInfoFunctionWebBu;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private WebFunctionUtil webFunctionUtil;

    /**
     * 查询权限表控制层
     *
     * @param
     * @param
     */
    @GetMapping("/getAppRoleInfoFunction")
    public SzResult AppRoleInfoFunction(
            @RequestParam(required = false) String page,
            @RequestParam(required = false) String pageSize,
            @RequestParam(required = false) String functionName,
            HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        return roleInfoFunctionBu.getRoleFunction(page, pageSize, functionName);
    }

    /**
     * 修改roleInfoFunction表中的WEB_FUNCTION_BELONG_ROLE_LEVEL数据所有数据控制层
     *
     * @param jsonParam：{functionNum:xxx,functionBelongRoleLevel:xxx} functionNum：权限编号
     *                                                                functionBelongRoleLevel：权限等级
     *                                                                state：0：新增，1：删除
     * @param
     */
    @PostMapping("/updateRoleInfoFunctionLevelApp")
    @PermissionCheck("function_updateRoleInfoFunctionLevelApp")
    public SzResult updateRoleInfoFunctionLevelApp(@RequestBody JSONObject jsonParam, HttpServletRequest request) throws Exception {
//    String token,String functionNum,String functionBelongRoleLevel
        String token = request.getHeader("Authorization");
        String functionNum = jsonParam.getString("functionNum");
        String functionBelongRoleLevel = jsonParam.getString("functionBelongRoleLevel");
        String state = jsonParam.getString("state");
        String plantId = jsonParam.getString("Plant-Id");
        return roleInfoFunctionBu.updateRoleFeunctionLevel(token, functionNum, functionBelongRoleLevel, state, plantId);
    }

    /**
     * 修改roleInfoFunction表中的WEB_FUNCTION_BELONG_ROLE_LEVEL数据所有数据控制层
     * @param   jsonParam：{functionNum:xxx,functionBelongRoleLevel:xxx}
     *                   functionNum：权限编号
     *                   functionBelongRoleLevel：权限等级
     *                   state：0：新增，1：删除
     * @param
     */
/*    @PostMapping("/updateRoleInfoFunctionAppLevel")
    public SzResult updateRoleInfoFunctionAppLevel(@RequestBody JSONObject jsonParam, HttpServletRequest request) throws Exception {
//    String token,String functionNum,String functionBelongRoleLevel
        String token = request.getHeader("Authorization");
        String functionNum = jsonParam.getString("functionNum");
        String functionBelongRoleLevel = jsonParam.getString("functionBelongRoleLevel");
        String state = jsonParam.getString("state");
        return roleInfoFunctionWebBu.updateRoleFunctionLevel(token,functionNum,functionBelongRoleLevel,state);
    }*/
//    =============================================web====================================================

    /**
     * 查询权限表控制层
     *
     * @param
     * @param
     */
    @GetMapping("/getRoleInfoFunction")
    public SzResult RoleInfoFunction(
            @RequestParam(required = false) String page,
            @RequestParam(required = false) String pageSize,
            @RequestParam(required = false) String functionName,
            HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        return roleInfoFunctionWebBu.getRoleFunction(page, pageSize, functionName);
    }
    /**
     * 修改roleInfoFunction表中的WEB_FUNCTION_BELONG_ROLE_LEVEL数据所有数据控制层
     * @param   jsonParam：{functionNum:xxx,functionBelongRoleLevel:xxx}
     *                   functionNum：权限编号
     *                   functionBelongRoleLevel：权限等级
     *                   state：0：新增，1：删除
     * @param
     */
/*    @PostMapping("/updateRoleInfoFunctionLevel")
    public SzResult updateRoleInfoFunctionLevel(@RequestBody JSONObject jsonParam, HttpServletRequest request) throws Exception {
//    String token,String functionNum,String functionBelongRoleLevel
        String token = request.getHeader("Authorization");
        String functionNum = jsonParam.getString("functionNum");
        String functionBelongRoleLevel = jsonParam.getString("functionBelongRoleLevel");
        String state = jsonParam.getString("state");
        if (!userInfoDao.getUserPermissionWeb(StpUtil.getLoginIdByToken(token).toString()).containsKey("function_updateRoleInfoFunctionLevel")) {
            return SzResult.error("您没有修改WEB端权限表角色等级的权限");
        }
        return roleInfoFunctionWebBu.updateRoleFunctionLevel(token,functionNum,functionBelongRoleLevel,state);
    }*/

    /**
     * 修改roleInfoFunction表中的WEB_FUNCTION_BELONG_ROLE_LEVEL数据所有数据控制层
     *
     * @param jsonParam：{functionNum:xxx,functionBelongRoleLevel:xxx} functionNum：权限编号
     *                                                                functionBelongRoleLevel：权限等级
     *                                                                state：0：新增，1：删除
     * @param
     */
    @PostMapping("/updateRoleInfoFunctionWebLevel")
    @PermissionCheck("function_updateRoleInfoFunctionWebLevel")
    public SzResult updateRoleInfoFunctionWebLevel(@RequestBody JSONObject jsonParam, HttpServletRequest request) throws Exception {
//    String token,String functionNum,String functionBelongRoleLevel
        String token = request.getHeader("Authorization");
        String functionNum = jsonParam.getString("functionNum");
        String functionBelongRoleLevel = jsonParam.getString("functionBelongRoleLevel");
        String state = jsonParam.getString("state");
        String plantId = request.getHeader("Plant-Id");
        return roleInfoFunctionWebBu.updateRoleFunctionLevel(token, functionNum, functionBelongRoleLevel, state, plantId);
    }

    /**
     * 查询所有角色
     *
     * @return
     */
    @GetMapping("/getAllRole")
    public SzResult getAllRole(
            @RequestParam(value = "roleName", required = false) String roleName,
            HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        return roleBu.getAllRole(roleName);
    }

    /**
     * 根据id查询用户app权限
     *
     * @return
     */
    @GetMapping("/getRoleAuthority")
    public SzResult getAppRoleAuthority(@RequestHeader("Authorization") String token,@RequestParam(value = "userId", required = false) String userId) throws Exception {
        return roleInfoFunctionBu.getAuthority(userId);
    }

    /**
     * 根据id查询角色app权限
     *
     * @return
     */
    @GetMapping("/getAppAuthority")
    public SzResult getAppAuthority(@RequestHeader("Authorization") String token,@RequestParam(value = "id", required = false) String id) throws Exception {
        return roleInfoFunctionBu.getAppAuthority(id);
    }


    /**
     * 根据id查询用户web权限
     *
     * @return
     */
    @GetMapping("/getWebRoleAuthority")
    public SzResult getWebRoleAuthority(@RequestHeader("Authorization") String token,@RequestParam(value = "userId", required = false) String userId) throws Exception {
        return roleInfoFunctionWebBu.getAuthority(userId);
    }

    /**
     * 根据id查询角色web权限
     *
     * @return
     */
    @GetMapping("/getWebAuthority")
    public SzResult getWebAuthority(@RequestHeader("Authorization") String token,@RequestParam(value = "id", required = false) String id) throws Exception {
        return roleInfoFunctionWebBu.getWebAuthority(id);
    }





}
